class AgentController < ApplicationController


  verify :method => :post, :only => [ :destroy, :create, :update ], :redirect_to => { :action => :list }

  paginate :agents, :per_page => 20

  def list
    get_agents
  end

  def new
    @agent = Agent.new
  end

  def create
    @agent = Agent.new params[:agent]
    if !@agent.save
      render :action => 'new'
    end
    flash[:notice] = _('Agent was successfully created.')
    get_agents
  end

  def update
    @agent = get_agent
    if @agent.update_attributes(params[:agent])
      flash[:notice] = _('Agent was successfully updated.')
      redirect_to :action => 'show', :id => @agent
    else
      render :action => 'edit'
    end
  end

  def show
    @agent = get_agent
  end

  def edit
    @agent = get_agent
  end

  def assign
    get_employee.agents << Agent.find(get_agent_list)
    flash[:notice] = _('Agents was successfully assigned')
  end



  private

  def get_agent
    Agent.find(params[:id])
  end

  def get_agent_list
    params[:agents].select{ |k,v| v==1}.map{|k,v| k }
  end

  def get_agents
    @agents=session[:user].agents
  end

  def get_employee
    Employee.find(params[:employee_id])
  end

end
